package com.mstore.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import com.mstore.pojo.Account;
import com.mstore.pojo.User;

/**
 * cookie的增加、删除、查询
 */
public class CookieUtils {

	public static final String ACCOUNT_COOKIE = "com.mstore.util.CookieUtils.account.cookie";

	public static final String USER_COOKIE = "com.mstore.util.CookieUtils.user.cookie";
	
	public static final String LOGIN_NAME = "com.mstore.util.CookieUtils.loginname.cookie";

	// public static final String COOKIE_DOMAIN = ".wumi.net";

	/**
	 * 添加loginName到cookie
	 */
	public Cookie addLoginNameInCookie(String loginName) {
		try {
			Cookie cookie = new Cookie(LOGIN_NAME, URLEncoder.encode(loginName, "UTF-8"));
			cookie.setMaxAge(60 * 60 * 24 * 2);// cookie保存2天
			return cookie;
		}
		catch (UnsupportedEncodingException e) {
			Cookie cookie = new Cookie(LOGIN_NAME, loginName);
			cookie.setMaxAge(60 * 60 * 24 * 2);// cookie保存2天
			return cookie;
		}
	} 
	
	/**
	 * 从cookie中取出loginName
	 */
	public String getLoginNameFromCookie(HttpServletRequest request) {
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (LOGIN_NAME.equals(cookie.getName())) {
					String value = cookie.getValue();
					if (StringUtils.isNotBlank(value))
						try {
							return URLDecoder.decode(value, "UTF-8");
						}
						catch (UnsupportedEncodingException e) {
							return value;
						}
				}
			}
		}
		return null;
	}
	
	/**
	 * 添加Account到cookie中
	 */
	public Cookie addAccountInCookie(Account account) {
		Cookie cookie = new Cookie(ACCOUNT_COOKIE, account.getMobile());
		cookie.setMaxAge(60 * 60 * 24 * 2);// cookie保存2天
		// cookie.setPath("/");
		// cookie.setDomain(COOKIE_DOMAIN);
		return cookie;
	}

	/**
	 * 从cookie中取出Account
	 */
	public Account getAccountFromCookie(HttpServletRequest request) {
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (ACCOUNT_COOKIE.equals(cookie.getName())) {
					String value = cookie.getValue();
					if (StringUtils.isNotBlank(value)) {
						Account account = new Account();
						account.setMobile(value);
						return account;
					}
				}
			}
		}
		return null;
	}

	/**
	 * 添加User到cookie中
	 */
	public Cookie addUserInCookie(User user) {
		try {
			String loginName = URLEncoder.encode(user.getLoginName(), "UTF-8");
			String paramKey = URLEncoder.encode(user.getParamKey(), "UTF-8");
			Cookie cookie = new Cookie(USER_COOKIE, loginName + "," + paramKey);
			cookie.setMaxAge(60 * 60 * 24 * 2);// cookie保存2天
			return cookie;
		}
		catch (UnsupportedEncodingException e) {
			Cookie cookie = new Cookie(USER_COOKIE, user.getLoginName() + ","
					+ user.getParamKey());
			cookie.setMaxAge(60 * 60 * 24 * 2);// cookie保存2天
			return cookie;
		}
	}

	/**
	 * 从cookie中取出User
	 */
	public User getUserFromCookie(HttpServletRequest request) {
		try {
			Cookie[] cookies = request.getCookies();
			if (cookies != null) {
				for (Cookie cookie : cookies) {
					if (USER_COOKIE.equals(cookie.getName())) {
						String value = cookie.getValue();
						if (StringUtils.isNotBlank(value)) {
							String[] split = value.split(",");
							User user = new User();
							user.setLoginName(URLDecoder.decode(split[0],
									"UTF-8"));
							user.setParamKey(URLDecoder.decode(split[1],
									"UTF-8"));
							return user;
						}
					}
				}
			}
		}
		catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 删除cookie
	 */
	public Cookie delCookie(HttpServletRequest request, String cookieName) {
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (cookieName.equals(cookie.getName())) {
					cookie.setValue("");
					cookie.setMaxAge(0);
					return cookie;
				}
			}
		}
		return null;
	}
}